﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Acms.Bll;

namespace Acms.AcmsControls.ContentControls.FrontEnd
{
    public abstract class BasePostPropertyControl : Control
    {

        public string Path { get; set; }

        /// <summary>
        /// Find containing post or current post
        /// </summary>
        /// <returns></returns>
        protected Post FindCurrentPost()
        {
            if (string.IsNullOrEmpty(Path))
            {
                var post = FindCurrentPost(this.NamingContainer);
                if (post == null && this.Page is TemplatePage)
                {
                    return ((TemplatePage)this.Page).AcmsContext.CurrentPost;
                }
                else
                {
                    return post;
                }
            }
            else
            {
                var context = FindAcmsContext(this.NamingContainer);
                var post = Post.GetBySiteIdAndPath(context.SiteId, Path, context.SiteVersion);
                return post;
            }
        }

        protected Acms.AcmsControls.AcmsContext FindAcmsContext(Control ctrl)
        {
            if (ctrl is TemplateUserControl)
            {
                return ((TemplateUserControl)ctrl).AcmsContext;
            }
            else if (ctrl is TemplatePage)
            {
                return ((TemplatePage)ctrl).AcmsContext;
            }
            else if (ctrl.NamingContainer != null)
            {
                return FindAcmsContext(ctrl.NamingContainer);
            }
            else
            {
                throw new ApplicationException("Unable to find AcmsContext as Naming Container of TemplateUserControl and TemplatePage not found");
            }

        }

        private Post FindCurrentPost(Control ctrl)
        {
            if (ctrl is IDataItemContainer && ((System.Web.UI.IDataItemContainer)ctrl).DataItem is Post)
            {
                var post = (Post)((IDataItemContainer)ctrl).DataItem;
                if (post is DummyPost)
                    return null;
                else
                    return post;
            }
            else if (ctrl.NamingContainer != null)
            {
                return FindCurrentPost(ctrl.NamingContainer);
            }
            else
            {
                return null;
            }
        }

        protected int FindCurrentIndex()
        {
            Control current = this;
            int? tmp = null;
            while (current != null && tmp == null)
            {
                current = current.NamingContainer;
                if (current is IDataItemContainer)
                {
                    tmp = ((IDataItemContainer)current).DataItemIndex;
                }
                else if (current is Telerik.Web.UI.GridItem)
                {
                    tmp = ((Telerik.Web.UI.GridItem)current).ItemIndex;
                }
            }
            return tmp ?? 0;
        }

        private bool RequiresDataBinding = true;

        protected override void OnPreRender(EventArgs e)
        {
            if (RequiresDataBinding)
            {
                RequiresDataBinding = false;
                DataBind();
            }
            base.OnPreRender(e);
        }

        public override void DataBind()
        {
            this.RequiresDataBinding = false;
            base.DataBind();
        }


        protected override object SaveViewState()
        {
            ViewState["RequiresDataBinding"] = RequiresDataBinding;
            ViewState["Path"] = Path;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            RequiresDataBinding = (bool)ViewState["RequiresDataBinding"];
            Path = (string)ViewState["Path"];
        }
   }


}
